<?php

namespace app\websocket\controller;
use Swoole\Coroutine\Http\Client;
class WebsocketClient
{
    public $client;
    public function __construct(){
//        $this->client= new \swoole_client(SWOOLE_SOCK_TCP);//默认同步tcp客户端，添加参数SWOOLE_SOCK_ASYNC为异步
        $this->client = new Client('127.0.0.1', 9001);
        $res=$this->client->upgrade('/');
        if($res){
            $this->client->push('hello');
        }
//        if (!$this->client->connect('127.0.0.1', 9001, -1)) {
//            exit("connect failed. Error: {$this->client->errCode}\n");
//        }
    }
//    public function connect(){
//        if(!$this->client->connect('127.0.0.1',9001,5)){
//            throw new Exception(sprintf('Swoole Error: %s', $this->client->errCode));
//        }
//    }
    public function send($data){
        if($this->client->isConnected()){
            $data = json_encode($data);
            //print $data;
            if($this->client->send($data)){
                return 1;
            }else{
                throw new Exception(sprintf('Swoole Error: %s', $this->client->errCode));
            }
        }else{
            throw new Exception('Swoole Server does not connected.');
        }
    }
    public function close(){
        $this->client->close();
    }
}